import difflib
import time
import os

def compare():
    try:
        f1 = r"First_File_Path"  # 获取第一个文件名
        f2 = r"Second_File_Path"  # 获取第二个文件名
    except Exception as e:
        print("Error: " + str(e))


    with open(f1, "r") as file1, open(f2, "r") as file2:
        text1 = file1.read().splitlines()
        text2 = file2.read().splitlines()

    d = difflib.HtmlDiff()  # 创建一个HtmlDiff实例
    diff_html = d.make_file(text1, text2)  # 生成HTML差异报告

    # 将差异报告写入文件
    diff_file = open('diff_{}_.html'.format(time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime())), "w")
    diff_file.write(diff_html)
    print("The file on {}".format(os.path.abspath(str(diff_file.name))))  # 提示文件生成在什么地方
    diff_file.close()

if __name__ == "__main__":
    compare()